<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <title> 小程序管理系统</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="format-detection" content="telephone=no">

  <link rel="stylesheet" href="__PUBLIC__/layui/css/layui.css"  media="all">
  <style type="text/css">
    .layui-table{
      text-align: center;
    }
    .layui-table td, .layui-table th{
      text-align: center;
    }
  </style>
</head>
<body>
<div class="layui-layout layui-layout-admin">

  <!--菜单列表-->
  <div id = "divhead"></div>
  <div class="layui-body">
    <!-- 内容主体区域 -->
    <div style="padding:0 15px;">
      <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend>闲置管理</legend>
      </fieldset>

      <div id="view"></div>
    </div>
  </div>

  <div class="layui-footer">
    <!-- 底部固定区域 -->
  </div>
</div>

<script id="common_tpl" type="text/html">

  <table class="layui-table">
    <thead>
    <tr>
      <th>ID</th>
      <th style = "width:120px;">上传者</th>
      <th>闲置名称</th>
      <th>闲置内容</th>
      <th>几成新</th>
      <th>分类</th>
      <th>想卖多少</th>
      <th>原价</th>
      <th>包邮</th>
      <th>运费</th>
      <th>添加时间</th>
      <th>操作</th>
    </tr>
    </thead>
    <tbody>
    {{#  layui.each(d, function(index, item){ }}
    <tr>
      <td>{{ item.id }}</td>
      <td>{{ item.nickname }}</td>
      <td>{{ item.unuse_name }}</td>
      <td>{{ item.unuse_info }}</td>
      <td>{{ item.is_new }} </td>
      <td>{{ item.unuse_type }}</td>
      <td>{{ item.price }}</td>
      <td>{{ item.original_price }}</td>
      <td>{{ item.is_free_post }}</td>
      <td>{{ item.freight }}</td>
      <td>{{ item.time }}</td>
      <td style = "width:250px;">
        {{#  if(item.is_tuijian == 0){ }}
        <button data-id="{{ item.id }}" class="tuijian layui-btn layui-btn-small">推荐</button>
        {{#  }else{ }}
        <button data-id="{{ item.id }}" class="tuijian layui-btn layui-btn-small">不推荐</button>
        {{#  } }}
        <button data-id="{{ item.id }}" class="edit-book layui-btn layui-btn-small">编辑</button>
        <button data-id="{{ item.id }}" class="del layui-btn layui-btn-small">删除</button>
      </td>
    </tr>
    {{#  }); }}
    </tbody>
  </table>
  <div id="page-box"></div>
</script>


<script src="__PUBLIC__/layui/layui.js" charset="utf-8"></script>
<script src="__PUBLIC__/js/jquery-1.11.1.min.js" charset="utf-8"></script>
<script>
    $(function(){
        $('#divhead').load('/index.php/Tongcheng/Index/menu/wid/2.html');
    })
    //JavaScript代码区域
    layui.use(['element','laypage','laytpl','layer'], function(){
        var element = layui.element
            ,$ = layui.jquery
            ,layer = layui.layer
            ,laypage = layui.laypage
            ,laytpl = layui.laytpl;
        var count = $("#count").val();


        // laypage.render({
        //     elem: 'page-box'
        //     ,count: count
        //     ,layout: ['count', 'prev', 'page', 'next', 'limit', 'skip']
        //     ,jump: function(obj){
        //         console.log(obj)
        //     }
        // });
        $("#view").on('click', '.edit-book', function(event) {
            var id = $(this).data('id');
            layer.open({
                type: 2,
                title: '编辑商品',
                shadeClose: true,
                shade: false,
                maxmin: true, //开启最大化最小化按钮
                area: ['400px', '600px'],
                content: '{:U("edit")}?id='+ id
            });
        });
        $("#view").on('click', '.tuijian', function(event) {
            var id = $(this).data('id');
            $.get("/index.php/Tongcheng/UnuseBack/tuijian?id=" + id, function (res) {
                if(1 == res){
                    layer.alert('修改成功');
                    setTimeout(function(){
                        window.location.reload();
                        // var index = parent.layer.getFrameIndex(window.name);
                        // parent.layer.close(index);
                    },1000);
                }else{
                    layer.alert('修改失败');
                }
            })
        });
        $("#view").on('click', '.del', function(event) {
            var id = $(this).data('id');
            layer.confirm('你确定要删除该商品？', {
                btn: ['确定','取消'] //按钮
            }, function(){
                $.get('{:U("del")}?id='+ id,function(e){
                    if(1 == e){
                        layer.alert('删除成功');
                        setTimeout(function(){
                            location.reload();
                        },1000);
                    }else{
                        layer.alert('删除失败');
                    }
                })
            },function(){
            });

        });

        getList(1, 10);

        function getList(page, limit){
            $.get("{:U('index')}?page="+page+"&limit="+limit,function(data){
                var view = document.getElementById('view');
                var getTpl = common_tpl.innerHTML;
                laytpl(getTpl).render(data.list, function(html){
                    view.innerHTML = html;
                });
                laypage.render({
                    elem: 'page-box'
                    ,count: data.count
                    ,limit:limit
                    ,curr: page
                    ,layout: ['count', 'prev', 'page', 'next', 'limit', 'skip']
                    ,jump: function(obj, first){
                        console.log(obj)
                        if(!first){
                            getList(obj.curr, obj.limit);
                        }
                    }
                });
            })
        }
    });
</script>
</body>
</html>
